﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using IPMSG;
using System.Windows.Threading;


namespace IPMsgMoTest
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.Items = new ObservableCollection<ItemViewModel>();
        }

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<ItemViewModel> Items { get; private set; }

        private string _sampleProperty = "Sample Runtime Property Value";
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding
        /// </summary>
        /// <returns></returns>
        public string SampleProperty
        {
            get
            {
                return _sampleProperty;
            }
            set
            {
                if (value != _sampleProperty)
                {
                    _sampleProperty = value;
                    NotifyPropertyChanged("SampleProperty");
                }
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void BindData()
        {
            // Sample data; replace with real data
            EventPublicator.Instance.UserEntryEvent += new EventHandler<UserEventArgs>(Instance_UserEntryEvent);
            EventPublicator.Instance.UserExitEvent += new EventHandler<UserEventArgs>(Instance_UserExitEvent);
            EventPublicator.Instance.MessageRcvEvent += new EventHandler<MessageRcvArgs>(Instance_MessageRcvEvent);
            this.IsDataLoaded = true;
        }

        /// <summary>
        /// 用户上线，增加项
        /// </summary>
        /// <param name="sender">暂无意义</param>
        /// <param name="e">用户信息</param>
        void Instance_UserEntryEvent(object sender, UserEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                this.Items.Add(new ItemViewModel() { HostName = e.HostName, Ip = e.Ip, UserName = e.UserName });
            });
        }

        /// <summary>
        /// 用户离线，删除项
        /// </summary>
        /// <param name="sender">暂无意义</param>
        /// <param name="e">用户信息</param>
        void Instance_UserExitEvent(object sender, UserEventArgs e)
        {
            foreach (ItemViewModel itemViewModel in this.Items)
            {
                if (itemViewModel.HostName == e.HostName)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        this.Items.Remove(itemViewModel);
                    });
                }
            }

            IPMsgMoTest.ViewModels.ViewManager.Instance.RemoveChatViewModel(e.HostName);
        }

        /// <summary>
        /// 收到信息
        /// </summary>
        /// <param name="sender">暂无意义</param>
        /// <param name="e">消息</param>
        void Instance_MessageRcvEvent(object sender, MessageRcvArgs e)
        {
            IPMsgMoTest.ViewModels.ViewManager.Instance.HandleRcvMsg(e.HostName, e.Message);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        ~MainViewModel()
        {
            System.Diagnostics.Debug.WriteLine("MainViewModel");
        }
    }
}